package com.example.xiao.httpprocessor.HttpProcessor

import android.os.Handler
import okhttp3.*
import java.io.IOException


class OKHttpPrpcessor : IHttpProcessor{

    private var mOkHttpClient: OkHttpClient? = null
    private var myHandler: Handler? = null

    init {
        mOkHttpClient = OkHttpClient()
        myHandler = Handler()
    }

    override fun post(url: String, params: Map<String, Any>, callback: ICallBack) {
        var request: Request = Request.Builder().url(url).build()
        mOkHttpClient!!.newCall(request).enqueue(object : Callback{
            override fun onFailure(call: Call?, e: IOException?) {
                callback.onFailure(e.toString())
            }

            override fun onResponse(call: Call?, response: Response?) {
                if (response!!.isSuccessful){
                    val result: String = response.body().string()
                    myHandler!!.post {
                        callback.onSuccess(result)
                    }
                }
            }

        })
    }
}